package com.example.educationai.config;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public String handleMaxSizeException(MaxUploadSizeExceededException e, RedirectAttributes redirectAttributes) {
        log.warn("文件大小超出限制", e);
        redirectAttributes.addFlashAttribute("error", "文件大小超出限制");
        return "redirect:/exam-submit";
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public String handleIllegalArgumentException(IllegalArgumentException e, RedirectAttributes redirectAttributes) {
        log.warn("参数错误", e);
        redirectAttributes.addFlashAttribute("error", e.getMessage());
        return "redirect:/exam-submit";
    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    public String handleMissingServletRequestParameterException(MissingServletRequestParameterException e, RedirectAttributes redirectAttributes, HttpServletRequest request) {
        log.warn("缺少必要的请求参数: {}", e.getMessage());
        redirectAttributes.addFlashAttribute("error", "请填写所有必填字段");
        
        // 根据请求的URL决定重定向目标
        String requestURI = request.getRequestURI();
        if (requestURI.startsWith("/exam/create")) {
            return "redirect:/exam/create";
        } else if (requestURI.startsWith("/exam/edit")) {
            return "redirect:" + requestURI;
        } else if (requestURI.startsWith("/exam/grade")) {
            return "redirect:/exam/grade";
        } else {
            return "redirect:/simple-login";
        }
    }

    @ExceptionHandler(MissingServletRequestPartException.class)
    public String handleMissingServletRequestPartException(MissingServletRequestPartException e, RedirectAttributes redirectAttributes, HttpServletRequest request) {
        log.warn("缺少必要的文件上传: {}", e.getMessage());
        redirectAttributes.addFlashAttribute("error", "请上传试卷图片");
        
        // 根据请求的URL决定重定向目标
        String requestURI = request.getRequestURI();
        if (requestURI.startsWith("/exam/grade")) {
            return "redirect:/exam/grade";
        } else if (requestURI.startsWith("/exam/submit")) {
            return "redirect:/exam/submit";
        } else {
            return "redirect:/exam/manage";
        }
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void handleNoHandlerFoundException(NoHandlerFoundException ex, HttpServletRequest request) throws NoHandlerFoundException {
        if (request.getRequestURI().equals("/favicon.ico")) {
            return;
        }
        throw ex;
    }

    @ExceptionHandler(Exception.class)
    public String handleException(Exception e, RedirectAttributes redirectAttributes, HttpServletRequest request) {
        log.error("系统错误", e);
        redirectAttributes.addFlashAttribute("error", "系统发生错误，请稍后重试");
        
        // 根据请求的URL决定重定向目标
        String requestURI = request.getRequestURI();
        if (requestURI.startsWith("/exam/")) {
            // 如果是考试相关的操作，重定向到考试管理页面
            return "redirect:/exam/manage";
        } else if (requestURI.startsWith("/class/")) {
            // 如果是班级相关的操作，重定向到班级列表页面
            return "redirect:/class/list";
        } else {
            // 其他情况重定向到登录页面
            return "redirect:/simple-login";
        }
    }
} 